CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(livesecurity)

SET(CMAKE_PROJECT_NAME "LiveSecurity")

INCLUDE(CheckIncludeFiles)
INCLUDE(CheckLibraryExists)
INCLUDE(CheckFunctionExists)
INCLUDE(FindPkgConfig)
INCLUDE(FeatureSummary)

INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src)
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/src)
SET(LS_SHARE_PATH ${CMAKE_INSTALL_PREFIX}/share/livesecurity)
SET(LS_LIB_PATH ${CMAKE_INSTALL_PREFIX}/lib)
SET(LS_BIN_PATH ${CMAKE_INSTALL_PREFIX}/bin)
FILE(GLOB_RECURSE ls_source "src/*.c")
FILE(GLOB_RECURSE ls_headers "src/*.h")
ADD_DEFINITIONS(-DSHARE_PATH="${LS_SHARE_PATH}")

OPTION(BUILD_APP "Build the main application" ON)
OPTION(BUILD_PLUGINS "Build plugins" OFF)
OPTION(BUILD_PLUGIN_BASH_HISTORY "Build Bash History plugin" OFF)
OPTION(BUILD_PLUGIN_MOD_SECURITY "Build Mod Security plugin" OFF)
OPTION(BUILD_PLUGIN_OSIRIS "Build Osiris plugin" OFF)
OPTION(BUILD_PLUGIN_P0F "Build P0F plugin" OFF)
OPTION(BUILD_PLUGIN_PIGA "Build PIGA plugin" OFF)
OPTION(BUILD_PLUGIN_SELINUX "Build SELinux plugin" OFF)
OPTION(BUILD_PLUGIN_SNORT "Build Snort plugin" OFF)
OPTION(BUILD_PLUGIN_SNORT_NET "Build Snort Network plugin" OFF)
OPTION(BUILD_PLUGIN_SYSLOG_SSH "Build Syslog SSH" OFF)
OPTION(BUILD_PLUGIN_SYSLOG_WGET "Build Syslog Wget" OFF)
## ADD AN OPTION HERE IF YOU WANT TO ADD A PLUGIN ##
OPTION(DEBUG "Debug mode" ON)

IF(NOT DEBUG)
	ADD_DEFINITIONS(-DNDEBUG -O4)
ELSE(NOT DEBUG)
	ADD_DEFINITIONS(-g)
ENDIF(NOT DEBUG)


PKG_SEARCH_MODULE(gthread gthread-2.0)
INCLUDE_DIRECTORIES(${gthread_INCLUDE_DIRS})
LINK_DIRECTORIES(${gthread_LIBRARY_DIRS})

PKG_SEARCH_MODULE(gtk2 gtk+-2.0)
INCLUDE_DIRECTORIES(${gtk2_INCLUDE_DIRS})
LINK_DIRECTORIES(${gtk2_LIBRARY_DIRS})

PKG_SEARCH_MODULE(rsvg librsvg-2.0)
INCLUDE_DIRECTORIES(${rsvg_INCLUDE_DIRS})
LINK_DIRECTORIES(${rsvg_LIBRARY_DIRS})

CHECK_LIBRARY_EXISTS(dl dlopen “” HAVE_LIBDL)


IF(BUILD_PLUGINS)
	SET(BUILD_PLUGIN_BASH_HISTORY ON)
	SET(BUILD_PLUGIN_MOD_SECURITY ON)
	SET(BUILD_PLUGIN_OSIRIS ON)
	SET(BUILD_PLUGIN_P0F ON)
	SET(BUILD_PLUGIN_PIGA ON)
	SET(BUILD_PLUGIN_SELINUX ON)
	SET(BUILD_PLUGIN_SNORT ON)
	SET(BUILD_PLUGIN_SNORT_NET ON)
	SET(BUILD_PLUGIN_SYSLOG_SSH ON)
	SET(BUILD_PLUGIN_SYSLOG_WGET ON)
	## TURN YOUR NEW PLUGIN OPTION TO ON HERE ##
ENDIF(BUILD_PLUGINS)




IF(BUILD_PLUGIN_BASH_HISTORY)
	FILE(GLOB_RECURSE bash_history_source "plugins/bash_history/*.c")
	FILE(GLOB_RECURSE bash_history_headers "plugins/bash_history/*.h")

	ADD_LIBRARY(bash_history MODULE ${bash_history_source} ${bash_history_headers})
	TARGET_LINK_LIBRARIES(bash_history ${gthread_LIBRARIES})
	TARGET_LINK_LIBRARIES(bash_history ${gtk2_LIBRARIES})
	TARGET_LINK_LIBRARIES(bash_history ${rsvg_LIBRARIES})
	TARGET_LINK_LIBRARIES(bash_history "dl")
	SET_TARGET_PROPERTIES(bash_history PROPERTIES
		COMPILE_FLAGS "-Wall"
		PREFIX "liblivesecurity"
		SUFFIX ".so")
	INSTALL(TARGETS bash_history DESTINATION ${LS_LIB_PATH})
	INSTALL(DIRECTORY "${CMAKE_SOURCE_DIR}/plugins/bash_history/data/" DESTINATION ${LS_SHARE_PATH}/plugins/bash_history/)
ENDIF(BUILD_PLUGIN_BASH_HISTORY)



IF(BUILD_PLUGIN_MOD_SECURITY)
	FILE(GLOB_RECURSE mod_security_source "plugins/mod_security/*.c")
	FILE(GLOB_RECURSE mod_security_headers "plugins/mod_security/*.h")

	ADD_LIBRARY(mod_security MODULE ${mod_security_source} ${mod_security_headers})
	TARGET_LINK_LIBRARIES(mod_security ${gthread_LIBRARIES})
	TARGET_LINK_LIBRARIES(mod_security ${gtk2_LIBRARIES})
	TARGET_LINK_LIBRARIES(mod_security ${rsvg_LIBRARIES})
	TARGET_LINK_LIBRARIES(mod_security "dl")
	SET_TARGET_PROPERTIES(mod_security PROPERTIES
		COMPILE_FLAGS "-Wall"
		PREFIX "liblivesecurity"
		SUFFIX ".so")
	INSTALL(TARGETS mod_security DESTINATION ${LS_LIB_PATH})
	INSTALL(DIRECTORY "${CMAKE_SOURCE_DIR}/plugins/mod_security/data/" DESTINATION ${LS_SHARE_PATH}/plugins/mod_security/)
ENDIF(BUILD_PLUGIN_MOD_SECURITY)



IF(BUILD_PLUGIN_OSIRIS)
	FILE(GLOB_RECURSE osiris_source "plugins/osiris/*.c")
	FILE(GLOB_RECURSE osiris_headers "plugins/osiris/*.h")

	ADD_LIBRARY(osiris MODULE ${osiris_source} ${osiris_headers})
	TARGET_LINK_LIBRARIES(osiris ${gthread_LIBRARIES})
	TARGET_LINK_LIBRARIES(osiris ${gtk2_LIBRARIES})
	TARGET_LINK_LIBRARIES(osiris ${rsvg_LIBRARIES})
	TARGET_LINK_LIBRARIES(osiris "dl")
	SET_TARGET_PROPERTIES(osiris PROPERTIES
		COMPILE_FLAGS "-Wall"
		PREFIX "liblivesecurity"
		SUFFIX ".so")
	INSTALL(TARGETS osiris DESTINATION ${LS_LIB_PATH})
	INSTALL(DIRECTORY "${CMAKE_SOURCE_DIR}/plugins/osiris/data/" DESTINATION ${LS_SHARE_PATH}/plugins/osiris/)
ENDIF(BUILD_PLUGIN_OSIRIS)



IF(BUILD_PLUGIN_P0F)
	FILE(GLOB_RECURSE p0f_source "plugins/p0f/*.c")
	FILE(GLOB_RECURSE p0f_headers "plugins/p0f/*.h")

	ADD_LIBRARY(p0f MODULE ${p0f_source} ${p0f_headers})
	TARGET_LINK_LIBRARIES(p0f ${gthread_LIBRARIES})
	TARGET_LINK_LIBRARIES(p0f ${gtk2_LIBRARIES})
	TARGET_LINK_LIBRARIES(p0f ${rsvg_LIBRARIES})
	TARGET_LINK_LIBRARIES(p0f "dl")
	SET_TARGET_PROPERTIES(p0f PROPERTIES
		COMPILE_FLAGS "-Wall"
		PREFIX "liblivesecurity"
		SUFFIX ".so")
	INSTALL(TARGETS p0f DESTINATION ${LS_LIB_PATH})
	INSTALL(DIRECTORY "${CMAKE_SOURCE_DIR}/plugins/p0f/data/" DESTINATION ${LS_SHARE_PATH}/plugins/p0f/)
ENDIF(BUILD_PLUGIN_P0F)



IF(BUILD_PLUGIN_PIGA)
	FILE(GLOB_RECURSE piga_source "plugins/piga/*.c")
	FILE(GLOB_RECURSE piga_headers "plugins/piga/*.h")

	ADD_LIBRARY(piga MODULE ${piga_source} ${piga_headers})
	TARGET_LINK_LIBRARIES(piga ${gthread_LIBRARIES})
	TARGET_LINK_LIBRARIES(piga ${gtk2_LIBRARIES})
	TARGET_LINK_LIBRARIES(piga ${rsvg_LIBRARIES})
	TARGET_LINK_LIBRARIES(piga "dl")
	SET_TARGET_PROPERTIES(piga PROPERTIES
		COMPILE_FLAGS "-Wall"
		PREFIX "liblivesecurity"
		SUFFIX ".so")
	INSTALL(TARGETS piga DESTINATION ${LS_LIB_PATH})
	INSTALL(DIRECTORY "${CMAKE_SOURCE_DIR}/plugins/piga/data/" DESTINATION ${LS_SHARE_PATH}/plugins/piga/)
ENDIF(BUILD_PLUGIN_PIGA)



IF(BUILD_PLUGIN_SELINUX)
	FILE(GLOB_RECURSE selinux_source "plugins/selinux/*.c")
	FILE(GLOB_RECURSE selinux_headers "plugins/selinux/*.h")

	ADD_LIBRARY(selinux MODULE ${selinux_source} ${selinux_headers})
	TARGET_LINK_LIBRARIES(selinux ${gthread_LIBRARIES})
	TARGET_LINK_LIBRARIES(selinux ${gtk2_LIBRARIES})
	TARGET_LINK_LIBRARIES(selinux ${rsvg_LIBRARIES})
	TARGET_LINK_LIBRARIES(selinux "dl")
	SET_TARGET_PROPERTIES(selinux PROPERTIES
		COMPILE_FLAGS "-Wall"
		PREFIX "liblivesecurity"
		SUFFIX ".so")
	INSTALL(TARGETS selinux DESTINATION ${LS_LIB_PATH})
	INSTALL(DIRECTORY "${CMAKE_SOURCE_DIR}/plugins/selinux/data/" DESTINATION ${LS_SHARE_PATH}/plugins/selinux/)
ENDIF(BUILD_PLUGIN_SELINUX)



IF(BUILD_PLUGIN_SNORT)
	FILE(GLOB_RECURSE snort_source "plugins/snort/*.c")
	FILE(GLOB_RECURSE snort_headers "plugins/snort/*.h")

	ADD_LIBRARY(snort MODULE ${snort_source} ${snort_headers})
	TARGET_LINK_LIBRARIES(snort ${gthread_LIBRARIES})
	TARGET_LINK_LIBRARIES(snort ${gtk2_LIBRARIES})
	TARGET_LINK_LIBRARIES(snort ${rsvg_LIBRARIES})
	TARGET_LINK_LIBRARIES(snort "dl")
	SET_TARGET_PROPERTIES(snort PROPERTIES
		COMPILE_FLAGS "-Wall"
		PREFIX "liblivesecurity"
		SUFFIX ".so")
	INSTALL(TARGETS snort DESTINATION ${LS_LIB_PATH})
	INSTALL(DIRECTORY "${CMAKE_SOURCE_DIR}/plugins/snort/data/" DESTINATION ${LS_SHARE_PATH}/plugins/snort/)
ENDIF(BUILD_PLUGIN_SNORT)



IF(BUILD_PLUGIN_SNORT_NET)
	FILE(GLOB_RECURSE snort_net_source "plugins/snort_net/*.c")
	FILE(GLOB_RECURSE snort_net_headers "plugins/snort_net/*.h")

	ADD_LIBRARY(snort_net MODULE ${snort_net_source} ${snort_net_headers})
	TARGET_LINK_LIBRARIES(snort_net ${gthread_LIBRARIES})
	TARGET_LINK_LIBRARIES(snort_net ${gtk2_LIBRARIES})
	TARGET_LINK_LIBRARIES(snort_net ${rsvg_LIBRARIES})
	TARGET_LINK_LIBRARIES(snort_net "dl")
	SET_TARGET_PROPERTIES(snort_net PROPERTIES
		COMPILE_FLAGS "-Wall"
		PREFIX "liblivesecurity"
		SUFFIX ".so")
	INSTALL(TARGETS snort_net DESTINATION ${LS_LIB_PATH})
	INSTALL(DIRECTORY "${CMAKE_SOURCE_DIR}/plugins/snort_net/data/" DESTINATION ${LS_SHARE_PATH}/plugins/snort_net/)
ENDIF(BUILD_PLUGIN_SNORT_NET)



IF(BUILD_PLUGIN_SYSLOG_SSH)
	FILE(GLOB_RECURSE syslog_ssh_source "plugins/syslog_ssh/*.c")
	FILE(GLOB_RECURSE syslog_ssh_headers "plugins/syslog_ssh/*.h")

	ADD_LIBRARY(syslog_ssh MODULE ${syslog_ssh_source} ${syslog_ssh_headers})
	TARGET_LINK_LIBRARIES(syslog_ssh ${gthread_LIBRARIES})
	TARGET_LINK_LIBRARIES(syslog_ssh ${gtk2_LIBRARIES})
	TARGET_LINK_LIBRARIES(syslog_ssh ${rsvg_LIBRARIES})
	TARGET_LINK_LIBRARIES(syslog_ssh "dl")
	SET_TARGET_PROPERTIES(syslog_ssh PROPERTIES
		COMPILE_FLAGS "-Wall"
		PREFIX "liblivesecurity"
		SUFFIX ".so")
	INSTALL(TARGETS syslog_ssh DESTINATION ${LS_LIB_PATH})
	INSTALL(DIRECTORY "${CMAKE_SOURCE_DIR}/plugins/syslog_ssh/data/" DESTINATION ${LS_SHARE_PATH}/plugins/syslog_ssh/)
ENDIF(BUILD_PLUGIN_SYSLOG_SSH)



IF(BUILD_PLUGIN_SYSLOG_WGET)
	FILE(GLOB_RECURSE syslog_wget_source "plugins/syslog_wget/*.c")
	FILE(GLOB_RECURSE syslog_wget_headers "plugins/syslog_wget/*.h")

	ADD_LIBRARY(syslog_wget MODULE ${syslog_wget_source} ${syslog_wget_headers})
	TARGET_LINK_LIBRARIES(syslog_wget ${gthread_LIBRARIES})
	TARGET_LINK_LIBRARIES(syslog_wget ${gtk2_LIBRARIES})
	TARGET_LINK_LIBRARIES(syslog_wget ${rsvg_LIBRARIES})
	TARGET_LINK_LIBRARIES(syslog_wget "dl")
	SET_TARGET_PROPERTIES(syslog_wget PROPERTIES
		COMPILE_FLAGS "-Wall"
		PREFIX "liblivesecurity"
		SUFFIX ".so")
	INSTALL(TARGETS syslog_wget DESTINATION ${LS_LIB_PATH})
	INSTALL(DIRECTORY "${CMAKE_SOURCE_DIR}/plugins/syslog_wget/data/" DESTINATION ${LS_SHARE_PATH}/plugins/syslog_wget/)
ENDIF(BUILD_PLUGIN_SYSLOG_WGET)



## COPY AND IF BLOCK HERE AND ADAPT IT TO YOUR NEW PLUGIN ##



IF(BUILD_APP)
	ADD_EXECUTABLE(livesecurity ${ls_source} ${ls_headers})
	TARGET_LINK_LIBRARIES(livesecurity ${gthread_LIBRARIES})
	TARGET_LINK_LIBRARIES(livesecurity ${gtk2_LIBRARIES})
	TARGET_LINK_LIBRARIES(livesecurity ${rsvg_LIBRARIES})
	TARGET_LINK_LIBRARIES(livesecurity "dl")
	SET_TARGET_PROPERTIES(livesecurity PROPERTIES
		COMPILE_FLAGS "-Wall -rdynamic")
	
	INSTALL(TARGETS livesecurity DESTINATION ${LS_BIN_PATH})
	INSTALL(FILES "${CMAKE_SOURCE_DIR}/data/icons/ls-menu-button.svg" DESTINATION "share/icons/hicolor/48x48/actions/")
	INSTALL(FILES "${CMAKE_SOURCE_DIR}/data/frame.ui" DESTINATION ${LS_SHARE_PATH})
	INSTALL(FILES "${CMAKE_SOURCE_DIR}/data/new-frame-dialog.ui" DESTINATION ${LS_SHARE_PATH})
	INSTALL(FILES "${CMAKE_SOURCE_DIR}/data/window.ui" DESTINATION ${LS_SHARE_PATH})
	INSTALL(FILES "${CMAKE_SOURCE_DIR}/data/livesecurity.conf" DESTINATION ${LS_SHARE_PATH})
	INSTALL(FILES "${CMAKE_SOURCE_DIR}/data/machine.conf" DESTINATION ${LS_SHARE_PATH})
	INSTALL(FILES "${CMAKE_SOURCE_DIR}/data/livesecurity.1.gz" DESTINATION "share/man/man1/")
	INSTALL(DIRECTORY "${CMAKE_SOURCE_DIR}/doc/html/" DESTINATION "${LS_SHARE_PATH}/doc")
ENDIF(BUILD_APP)


#PKG-CONFIG
#set(LS_VERSION "0.0.3")
#configure_file(data/livesecurity.pc.in data/livesecurity.pc @ONLY)
#install(FILES "${CMAKE_SOURCE_DIR}/data/livesecurity.pc" DESTINATION lib/livesecurity)


MESSAGE("Application:")
MESSAGE("    Build Application : ${BUILD_APP} (-DBUILD_APP=ON/OFF, default ON)")
MESSAGE("    Debug Mode        : ${DEBUG} (-DDEBUG=ON/OFF, default ON)")
MESSAGE("    Install Prefix    : '${CMAKE_INSTALL_PREFIX}' (-DCMAKE_INSTALL_PREFIX=/your/prefix to modify the prefix to /your/prefix)")
MESSAGE("")

MESSAGE("Plugins: (-DBUILD_PLUGINS=ON to build all plugins)")
MESSAGE("    Bash History      : ${BUILD_PLUGIN_BASH_HISTORY} (-DBUILD_PLUGIN_BASH_HISTORY=ON/OFF, default OFF)")
MESSAGE("    Mod_security      : ${BUILD_PLUGIN_MOD_SECURITY} (-DBUILD_PLUGIN_MOD_SECURITY=ON/OFF, default OFF)")
MESSAGE("    Osiris            : ${BUILD_PLUGIN_OSIRIS} (-DBUILD_PLUGIN_OSIRIS=ON/OFF, default OFF)")
MESSAGE("    P0F               : ${BUILD_PLUGIN_P0F} (-DBUILD_PLUGIN_P0F=ON/OFF, default OFF)")
MESSAGE("    PIGA-IDS          : ${BUILD_PLUGIN_PIGA} (-DBUILD_PLUGIN_PIGA=ON/OFF, default OFF)")
MESSAGE("    SELinux           : ${BUILD_PLUGIN_SELINUX} (-DBUILD_PLUGIN_SELINUX=ON/OFF, default OFF)")
MESSAGE("    Snort             : ${BUILD_PLUGIN_SNORT} (-DBUILD_PLUGIN_SNORT=ON/OFF, default OFF)")
MESSAGE("    Snort (network)   : ${BUILD_PLUGIN_SNORT_NET} (-DBUILD_PLUGIN_SNORT_NET=ON/OFF, default OFF)")
MESSAGE("    Syslog (SSH)      : ${BUILD_PLUGIN_SYSLOG_SSH} (-DBUILD_PLUGIN_SYSLOG_SSH=ON/OFF, default OFF)")
MESSAGE("    Syslog (Wget)     : ${BUILD_PLUGIN_SYSLOG_WGET} (-DBUILD_PLUGIN_SYSLOG_WGET=ON/OFF, default OFF)")
## ADD A MESSAGE LINE HERE FOR YOUR NEW PLUGIN ##
MESSAGE("")



MESSAGE("Run make to build the application (make install to install it).")
